#This file contains all code for parsing the csv file datasets.

import Post
import Probability

#Returns True if the end of the file stream has been reached.
def isEndOfFile(fileStream):
	if (len(fileStream.peek())==0):
		return True
	else:
		return False

#Reads characters from the filestream until the specified character is reached or the end of the file stream is reached.
#If the contents of the stream are surrounded by quotation marks, the target character does not count.
#If two quotations occur in a row, it is considered to be a literal quotaion mark. The first quotation mark acts as an escape character.
def readToCharacterOrEnd(fileStream,character):
	characterReached = False
	string = ""
	inQuotes = False
	while (not characterReached):
		if(isEndOfFile(fileStream)):
			break
		char = fileStream.read(1)
		if(char==character and (not inQuotes)):
			characterReached = True
		elif(char=="\""):
			nextChar = fileStream.read(1)
			if(nextChar=="\""):
				string = string+nextChar
			else:
				inQuotes = (not inQuotes)
				if(inQuotes):
					string = string + nextChar
				elif(nextChar==character):
					characterReached = True
		else:
			string = string + char
	return string

#Reads and entire post (all of its attributes) and returns a parsed post object
def readPost(fileStream):
	postId = readToCharacterOrEnd(fileStream,",")
	postCreationDate = readToCharacterOrEnd(fileStream,",")
	ownerUserId = readToCharacterOrEnd(fileStream,",")
	ownerCreationDate = readToCharacterOrEnd(fileStream,",")
	repuationAtPostCreation = readToCharacterOrEnd(fileStream,",")
	ownerUndeletedAnswerCountAtPostTime = readToCharacterOrEnd(fileStream,",")
	title = readToCharacterOrEnd(fileStream,",")
	bodyMarkdown = readToCharacterOrEnd(fileStream,",")
	tag1 = readToCharacterOrEnd(fileStream,",")
	tag2 = readToCharacterOrEnd(fileStream,",")
	tag3 = readToCharacterOrEnd(fileStream,",")
	tag4 = readToCharacterOrEnd(fileStream,",")
	tag5 = readToCharacterOrEnd(fileStream,",")
	postClosedDate = readToCharacterOrEnd(fileStream,",")
	openStatus = readToCharacterOrEnd(fileStream,"\n")
	post = Post.Post(postId,postCreationDate,ownerUserId,ownerCreationDate,repuationAtPostCreation,ownerUndeletedAnswerCountAtPostTime,title,bodyMarkdown,tag1,tag2,tag3,tag4,tag5,postClosedDate,openStatus)
	return post
	
def readLeaderboardPost(fileStream):
	postId = readToCharacterOrEnd(fileStream,",")
	postCreationDate = readToCharacterOrEnd(fileStream,",")
	ownerUserId = readToCharacterOrEnd(fileStream,",")
	ownerCreationDate = readToCharacterOrEnd(fileStream,",")
	repuationAtPostCreation = readToCharacterOrEnd(fileStream,",")
	ownerUndeletedAnswerCountAtPostTime = readToCharacterOrEnd(fileStream,",")
	title = readToCharacterOrEnd(fileStream,",")
	bodyMarkdown = readToCharacterOrEnd(fileStream,",")
	tag1 = readToCharacterOrEnd(fileStream,",")
	tag2 = readToCharacterOrEnd(fileStream,",")
	tag3 = readToCharacterOrEnd(fileStream,",")
	tag4 = readToCharacterOrEnd(fileStream,",")
	tag5 = readToCharacterOrEnd(fileStream,"\n")
	post = Post.Post(postId,postCreationDate,ownerUserId,ownerCreationDate,repuationAtPostCreation,ownerUndeletedAnswerCountAtPostTime,title,bodyMarkdown,tag1,tag2,tag3,tag4,tag5,"", "open")
	return post

def readProbabilityLine(fileStream,postIdGiven,actualStatus):
	#postId = readToCharacterOrEnd(fileStream,",")
	notARealQuestionPrediction = float(readToCharacterOrEnd(fileStream,",").strip())
	notConstructivePrediction = float(readToCharacterOrEnd(fileStream,",").strip())
	offTopicPrediction = float(readToCharacterOrEnd(fileStream,",").strip())
	openPrediction = float(readToCharacterOrEnd(fileStream,",").strip())
	tooLocalizedPrediction = float(readToCharacterOrEnd(fileStream,"\n").strip())
	totalPrediction = notARealQuestionPrediction + notConstructivePrediction + offTopicPrediction + openPrediction + tooLocalizedPrediction
	
	#if(postId != postIdGiven):
	#	print "ERROR - Post Ids do not match."
	
	normalizedOpenPrediction = openPrediction / totalPrediction
	normalizedOffTopicPrediction = offTopicPrediction / totalPrediction
	normalizedNotConstructivePrediction = notConstructivePrediction / totalPrediction
	normalizedNotARealQuestionPrediction = notARealQuestionPrediction / totalPrediction
	normalizedTooLocalizedPrediction = tooLocalizedPrediction / totalPrediction
	
	probability = Probability.Probability(postIdGiven,actualStatus,normalizedOpenPrediction,normalizedOffTopicPrediction,normalizedNotConstructivePrediction,normalizedNotARealQuestionPrediction,normalizedTooLocalizedPrediction)
	return probability
	
#Reads the header contained in the original dataset.
def readHeader(fileStream):
	count = 0
	while count < 14:
		readToCharacterOrEnd(fileStream,",")
		count = count + 1
	readToCharacterOrEnd(fileStream,"\n")

def simpleReadHeader(fileStream):
	readToCharacterOrEnd(fileStream,"\n")		